var url = require('url')
var fs = require('fs')
var mime = require('mime')
var _ = require('underscore')

module.exports = function(req, res){
	req.url = req.url.toLowerCase()
	req.method = req.method.toLowerCase()

	var urlObj = url.parse(req.url, true)
	req.query = urlObj.query

	req.pathname = urlObj.pathname

	res.render = function(filename, tplData){
		fs.readFile(filename, 'utf8', function(err, data){
			if (err) {
				throw err
			}
			if (tplData) {
				var fn = _.template(data)
				data = fn(tplData)
			}
			res.setHeader('Content-Type', mime.getType(filename))
			res.end(data)
		})
	}
}